/**
 * Created by yan on 16/2/24.
 */
$(function(){

    var name = $("#id1").text();
    if(name == "depponUser"){
        $("#id1").hide();
        $("#all").hide();
        $("#tb #exprotExcel").hide();
    }else{
        $("#id1").hide();
        $("#db").hide();
        $("#tb #exprotExcelDb").hide();
    }

    $('#dtmTree').treegrid({
         onDblClickRow: function (rowData) {
             window.open('/gateway/dayCardDetailManager.html?IccId='+rowData.iccid,'_blank');
         }
     });

    //分页
    var pager=$('#dtmTree').treegrid('getPager');
    pager.pagination({
        pageSize:10,
        pageList : [10,20],// 可以设置每页记录条数的列表
        beforePageText: '第',//页数文本框前显示的汉字 
        afterPageText: '页    共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
        onBeforeRefresh: function () {
        },
        onSelectPage: function (pageNumber, pageSize) {//分页触发
            getGridData(pageNumber, pageSize);
        }
    });
});

//ajax调用获取数据
function getGridData (pageNumber, pageSize) {

    $.ajax({
        type: "POST",
        dataType: "json",
        url: '/dtm/constructionTreeGrid',
        data: {
            'page': pageNumber,
            'rows': pageSize
        },
        success: function (data) {
            $("#dtmTree").treegrid('loadData', pageData(data.rows, data.total));//用来封装获取的总条数，和数据，data.rows是我在控制器里面添加的一个map集合的键的名称
        },
        error: function (err) {
            alert("失败");
        }
    });

}

function pageData(list,total){
    var obj=new Object();
    obj.total=total;
    obj.rows=list;
    return obj;
}



function myformatter(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}

function myparser(s) {
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0], 10);
    var m = parseInt(ss[1], 10);
    var d = parseInt(ss[2], 10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
        return new Date(y, m - 1, d);
    } else {
        return new Date();
    }
}

//德邦查询
function dtmSearchDb() {

    var siteName = $('#siteName').val();
    var occurredOnDb = $('#occurredOnDb').datebox('getValue');

    var name = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im;
    var date = /((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))/ig;

    //德邦界面
    if ((siteName.length == 0 || siteName.substring(0, siteName.length) == 0)
        && (occurredOnDb.length == 0 || occurredOnDb.substring(0, occurredOnDb.length) == 0)) {

        // alert("德邦输入需要查询的条件不能为空或空格");

        $('#dtmTree').treegrid({
            url: '/dtm/constructionTreeGrid',
            queryParams: {
                occurredOn: occurredOnDb,
                siteName: siteName
            }
        });

        $('#tg').datagrid({
            url: "/dtm/getInstanceToalByNameAndTime",
            queryParams: {
                occurredOn: occurredOnDb,
                siteName: siteName
            }
        });

    }else if(!date.test(occurredOnDb) && siteName.length==0) {

        alert("德邦输入需要查询的时间有误");

    }else if(name.test(siteName)){

        alert("德邦输入需要查询的条件含有非法字符！");

    }else if (siteName.length > 0 && occurredOnDb.length == 0) {

        // alert("德邦站点查询");

        $('#dtmTree').treegrid({
            url: '/dtm/constructionTreeGrid',
            queryParams: {
                occurredOn: occurredOnDb,
                siteName: siteName
            }
        });

        $('#tg').datagrid({
            url: "/dtm/getInstanceToalByNameAndTime",
            queryParams: {
                occurredOn: occurredOnDb,
                siteName: siteName
            }
        });

    }else if(siteName.length == 0 && occurredOnDb.length > 0){

        // alert("德邦时间查询");

        $('#dtmTree').treegrid({
            url: '/dtm/constructionTreeGrid',
            queryParams: {
                occurredOn: occurredOnDb,
                siteName: siteName
            }
        });

        $('#tg').datagrid({
            url: "/dtm/getInstanceToalByNameAndTime",
            queryParams: {
                occurredOn: occurredOnDb,
                siteName: siteName
            }
        });

    }else if(siteName.length > 0 && occurredOnDb.length > 0){

        // alert("德邦双向查询");

        $('#dtmTree').treegrid({
            url: '/dtm/constructionTreeGrid',
            queryParams: {
                occurredOn: occurredOnDb,
                siteName: siteName
            }
        });

        $('#tg').datagrid({
            url: "/dtm/getInstanceToalByNameAndTime",
            queryParams: {
                occurredOn: occurredOnDb,
                siteName: siteName
            }
        });

    }


}


//全部查询
function dtmSearchAll() {

    var contractName = $('#contractName').val();
    var occurredOn = $('#occurredOn').datebox('getValue');
    var name = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im;
    var date = /((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))/ig;

    //全部界面
    if ((contractName.length == 0 || contractName.substring(0, contractName.length) == 0)
        && (occurredOn.length == 0 || occurredOn.substring(0, occurredOn.length) == 0)) {

        alert("输入需要查询的条件不能为空或空格");

        $('#dtmTree').treegrid({
            url: '/dtm/constructionTreeGrid',
            queryParams: {
                occurredOn: occurredOn,
                contractName: contractName
            }
        });

        $('#tg').datagrid({
            url: "/dtm/getInstanceToalByNameAndTime",
            queryParams: {
                occurredOn: occurredOn,
                contractName: contractName
            }
        });

    }else if(!date.test(occurredOn) && contractName.length==0) {

        alert("输入需要查询的时间有误");

    }else if(name.test(contractName)){

        alert("输入需要查询的条件含有非法字符！");

    }else if (contractName.length > 0 && occurredOn.length == 0) {

        // alert("站点查询");


        $('#dtmTree').treegrid({
            url: '/dtm/constructionTreeGrid',
            queryParams: {
                occurredOn: occurredOn,
                contractName: contractName
            }
        });

        $('#tg').datagrid({
            url: "/dtm/getInstanceToalByNameAndTime",
            queryParams: {
                occurredOn: occurredOn,
                contractName: contractName
            }
        });

    }else if(contractName.length == 0 && occurredOn.length > 0){

        // alert("时间查询");


        $('#dtmTree').treegrid({
            url: '/dtm/constructionTreeGrid',
            queryParams: {
                occurredOn: occurredOn,
                contractName: contractName
            }
        });

        $('#tg').datagrid({
            url: "/dtm/getInstanceToalByNameAndTime",
            queryParams: {
                occurredOn: occurredOn,
                contractName: contractName
            }
        });

    }else if(contractName.length > 0 && occurredOn.length > 0){

        // alert("双向查询");

        $('#dtmTree').treegrid({
            url: '/dtm/constructionTreeGrid',
            queryParams: {
                occurredOn: occurredOn,
                contractName: contractName
            }
        });

        $('#tg').datagrid({
            url: "/dtm/getInstanceToalByNameAndTime",
            queryParams: {
                occurredOn: occurredOn,
                contractName: contractName
            }
        });
   }
}


//导入Excel文件
function importFile() {

    var excelPath=$("#excelPath").val();

    if(excelPath == null || excelPath == '') {
        alert("请选择要上传的Excel文件");
        return;
    }else{
        var fileExtend = excelPath.substring(excelPath.lastIndexOf('.')).toLowerCase();
        if(fileExtend == '.xls'){

            $.messager.confirm('询问提示', '确认导入Excel表格吗？', function (data) {

                if(data) {

                    $('#importExcel').form('submit', {
                        url: "/dtm/uploadFile",
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $.messager.progress('close');	// hide progress bar while the form is invalid
                            }
                            return isValid;	// return false will stop the form submission
                        },
                        success: function () {
                            alert("导入成功");
                            $('#importExcel').form('clear');
                            $('#upload').dialog('close');
                            $('#dtmTree').treegrid('reload');
                        }
                    });
                }
            });

        }else{
            alert("文件格式需为'.xls'格式");
            return;
        }
    }
}

//导出Excel文件
function ShowExport() {

    var contractName = $('#contractName').val();
    var occurredOn = $('#occurredOn').datebox('getValue');

    $.messager.confirm('询问提示', '要将数据导出Excel吗？', function (data) {

         if(data){

             $('#exprotExcel').form('submit',{
                 url:"/dtm/exportExcel/",
                 queryParams: {
                     occurredOn: occurredOn,
                     contractName: contractName
                 },
                 onSubmit: function () {
                     var isValid = $(this).form('validate');
                     if (!isValid) {
                         $.messager.progress('close');	// hide progress bar while the form is invalid
                     }
                     return isValid;	// return false will stop the form submission
                 },
                 success: function () {
                     alert("成功");
                 }
             });

         }else{
             return false;
         }

    });


}


//导出Excel文件(德邦)
function ShowExport() {

    var siteName = $('#siteName').val();
    var occurredOn = $('#occurredOn').datebox('getValue');

    $.messager.confirm('询问提示', '要将数据导出Excel吗？', function (data) {

        if(data){

            $('#exprotExcelDb').form('submit',{
                url:"/dtm/exportExcel/",
                queryParams: {
                    occurredOn: occurredOn,
                    siteName: siteName
                },
                onSubmit: function () {
                    var isValid = $(this).form('validate');
                    if (!isValid) {
                        $.messager.progress('close');	// hide progress bar while the form is invalid
                    }
                    return isValid;	// return false will stop the form submission
                },
                success: function () {
                    alert("成功");
                }
            });

        }else{
            return false;
        }

    });


}
